<?php
/**
 * Created by : PhpStorm
 * User: ZPF
 * Date: 2022/4/2
 * Time: 10:10
 */

namespace app\admin\controller;


use app\admin\model\AdminModel;
use app\admin\model\RoleModel;
use app\admin\services\BaseService;
use app\BaseController;

class AdminUser extends BaseController
{
    protected $middleware=['CheckTokenMiddleware'];
    public function list()
    {
        $p=$this->request->param();

        $params=[
            'where'=>[],
            'page'=>($p['pageNo'])??1,
            'pageSize'=>($p['pageSize'])??10,
            'withoutField'=>'pwd'
        ];
        $Admin=new AdminModel();
        if(!empty($p['role'])){
            $params['where'][]=['role','=',$p['role']];
        }
        $list=$Admin->getList($params)->toArray();
        foreach ($list as $key=> $item){
            $list[$key]['bind']=($item['openid'])?1:0;
        }
        $count=$Admin->count($params['where']);
        $roleIds=array_column($list,'role');
        $roleList=array_column(RoleModel::getList([['id','in',implode(',',$roleIds)]],1,1000),'role_name','id');
        api_return(200,'',['list'=>$list,'total'=>$count,'roleList'=>$roleList]);
    }
    public function save()
    {
        if($this->request->isPost()){
            $p = $this->request->post();
            if(!empty($p['pwd'])){
                if(strlen($p['pwd'])<6){
                    api_return(500,'密码不能小于6位');
                }
            }
            $res = AdminModel::editAdmin($p);
            api_return($res['code'],$res['msg']);
        }
        api_return(50001,'');
    }
    public function del()
    {
        $p=$this->request->post();
        $res=(AdminModel::del($p['ids']));
        api_return($res['code'],$res['msg']);
    }
    public function unbind(){
        $p=$this->request->post();
        AdminModel::edit($p);
    }
    public function getAdminListByRole(){
        $p=$this->request->param();
        $where=[
            BaseService::getAdminListPermission(AdminModel::find($this->request->adminUid),'id'),
        ];
        if(!empty($p['role'])){
            $where[]=['role','=',$p['role']];
        }
        if(!empty($p['shop_id'])){
            $where[]=['shop_id','=',$p['shop_id']];
        }
        $list=AdminModel::where($where)->field('id,nickname')->select();
        $total=count($list);
        api_return(200,'',['list'=>$list,'total'=>$total]);
    }
    public function avatar(){
        $p=$this->request->param();
        $admin_id = $this->request->adminUid;
        $base64Image = substr($p['avatar'], strpos($p['avatar'], ',') + 1);
        $imageData = base64_decode($base64Image);
        if ($imageData === false) {
            api_return(500,'解码失败',[]);
        }
        $savePath = app()->getRootPath(). "/public/static/upload/avatar/".$admin_id.".jpg"; // 保存路径和文件名
        file_put_contents($savePath, $imageData);
        AdminModel::editAdmin([
            'id'=>$admin_id,
            'avatar'=>"avatar/".$admin_id.".jpg"
        ]);
        api_return(200);
    }
}
